package com.yc.navy.dayfresh.controller;

import java.io.IOException;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yc.navy.dayfresh.bean.MemberInfo;
import com.yc.navy.dayfresh.enums.ResultEnum;
import com.yc.navy.dayfresh.service.IMemberInfoService;
import com.yc.navy.dayfresh.util.ConstantInfo;
import com.yc.navy.dayfresh.util.SendEmailUtil;
import com.yc.navy.dayfresh.vo.ResultVO;

@RestController//如果Controller,login那里会报错
@RequestMapping("/member")
public class MemberInfoController{
	@Autowired
	private SendEmailUtil sendEmailUtil;
	
	@Resource
	private IMemberInfoService memberInfoService;
	/**
	 * 发送邮箱验证码
	 * @param request
	 * @param response
	 * @return 
	 * @throws IOException 
	 */
	@RequestMapping("/sendCode")
	public ResultVO sendCode(String email,String nickName,HttpSession session) throws IOException {
		String code = "";
		Random rd = new Random();
		while(code.length() < 6) {
			code += rd.nextInt(10); 
		}
		
		if(sendEmailUtil.sendEmail(email, code, nickName)) {//说明邮件发送成功
			session.setAttribute(ConstantInfo.EMAILCHECKCODE, code);//存session  之前: request.getSession().setAttribute(ConstantInfo.EMAILCHECKCODE, code);//存session
			
			
			TimerTask task = new TimerTask() {
				@Override
				public void run() {
					session.removeAttribute(ConstantInfo.EMAILCHECKCODE);//之前:request.getSession().removeAttribute(ConstantInfo.EMAILCHECKCODE);
				}
			};
			
			Timer timer = new Timer();
			timer.schedule(task, 100000);//3分钟执行一次任务一次
//			以前:this.send(response, new ResultVO(ResultEnum.SUCCESS));
			return new ResultVO(ResultEnum.SUCCESS);
		}
//		以前:this.send(response, new ResultVO(ResultEnum.ERROR));
		return new ResultVO(ResultEnum.SUCCESS);
	}
	
	/**
	 * 注册
	 * @param request
	 * @param response
	 * @return 
	 * @throws IOException
	 */
	@RequestMapping("/reg")
	public ResultVO reg(String vcode,HttpSession session,MemberInfo mf) throws IOException {
		Object obj = session.getAttribute(ConstantInfo.EMAILCHECKCODE);
		if(obj == null ) {
//			之前:this.send(response, new ResultVO(ResultEnum.DATA_TIMEOUT));
			return new ResultVO(ResultEnum.DATA_TIMEOUT);
		}
		
		String code = (String) obj;
//		String vcode = request.getParameter("vcode");

		if(!code.equals(vcode)) {
//			this.send(response, new ResultVO(ResultEnum.CODE_ERROR));//验证码错误
			return new ResultVO(ResultEnum.CODE_ERROR);
		}
//		ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
//		IMemberInfoService memberInfoService =(IMemberInfoService) context.getBean("memberInfoServiceImpl");
		
		int result = memberInfoService.reg(mf);
		
		if(result == -1) {
//			this.send(response, new ResultVO(ResultEnum.CHECK_ERROR));//数据格式检验错误
			return new ResultVO(ResultEnum.CHECK_ERROR);
		}
		
		if(result > 0) {
//			this.send(response, new ResultVO(ResultEnum.SUCCESS));
			return new ResultVO(ResultEnum.SUCCESS);
		}
//		this.send(response,new ResultVO(ResultEnum.ERROR));//验证码错误
		return new ResultVO(ResultEnum.ERROR);
	}
	
	/**
	 * 用户登录
	 * @param request
	 * @param response
	 * @return 
	 * @throws IOException
	 */
	@PostMapping("/login")
	public ResultVO login(@RequestParam("code") String vcode,HttpSession session,MemberInfo mf) throws IOException {
		String code = (String) session.getAttribute(ConstantInfo.LOGIN_CODE);
		if(!code.equalsIgnoreCase(vcode)) {
			return new ResultVO(ResultEnum.CODE_ERROR);
		}
		
		MemberInfo member = memberInfoService.login(mf);
		if(member == null) {
			return new ResultVO(ResultEnum.ERROR);
		}
		session.setAttribute(ConstantInfo.CURRENTLOGINMEMBERINFO, member);
		return new ResultVO(ResultEnum.SUCCESS);
	}
	
	/**
	 * 获取登录用户信息
	 * @param request
	 * @param response
	 * @return 
	 * @throws IOException
	 */
	@GetMapping("/checkLogin")
	public ResultVO checkLogin(HttpSession session) throws IOException {
		Object obj = session.getAttribute(ConstantInfo.CURRENTLOGINMEMBERINFO);
		if(obj == null) {
			return new ResultVO(ResultEnum.LOGIN_ERROR);
		}
		return new ResultVO(ResultEnum.SUCCESS,obj);
	}
}
